{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<module 'torch' from '/home/wenbin/pyvenv/ml/lib/python3.7/site-packages/torch/__init__.py'>\n",
      "<module 'torchvision' from '/home/wenbin/pyvenv/ml/lib/python3.7/site-packages/torchvision/__init__.py'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([[0.1699, 0.2107, 0.6919, 0.4724, 0.1464],\n",
       "        [0.8945, 0.7386, 0.2728, 0.7790, 0.4416],\n",
       "        [0.6816, 0.7786, 0.6324, 0.7491, 0.7286]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "import torchvision\n",
    "print(torch)\n",
    "print(torchvision)\n",
    "torch.rand(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
      "        [0.0000e+00, 1.4013e-45, 0.0000e+00],\n",
      "        [0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
      "        [0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
      "        [0.0000e+00, 0.0000e+00, 0.0000e+00]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.empty(5, 3)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.zeros(5, 3, dtype=torch.long)\n",
    "print(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([5.5000, 3.0000])\n"
     ]
    }
   ],
   "source": [
    "x = torch.tensor([5.5, 3])\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.]], dtype=torch.float64)\n",
      "tensor([[ 0.4835,  0.3349, -0.7775],\n",
      "        [ 0.2293,  0.9957, -1.7917],\n",
      "        [ 0.1282, -0.3381, -0.8351],\n",
      "        [-0.0044,  0.5020,  0.0821],\n",
      "        [ 0.5042, -0.8222,  0.5606]])\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "x = x.new_ones(5, 3, dtype=torch.double)    \n",
    "\n",
    "# new_* methods take in sizes\n",
    "print(x)\n",
    "\n",
    "\n",
    "x = torch.randn_like(x, dtype=torch.float)  \n",
    "\n",
    "# override dtype!\n",
    "print(x)                                    \n",
    "\n",
    "\n",
    "result = torch.empty(5, 3)\n",
    "torch.add(x, y, out=result)\n",
    "print(result)\n",
    "\n",
    "\n",
    "# result has the same size\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.6145,  1.1860, -0.2074],\n",
      "        [ 0.9967,  1.4342, -1.6493],\n",
      "        [ 0.1644, -0.1836,  0.0405],\n",
      "        [ 0.4860,  1.1370,  0.6028],\n",
      "        [ 1.0587,  0.0252,  1.1134]])\n",
      "tensor([[ 0.6145,  1.1860, -0.2074],\n",
      "        [ 0.9967,  1.4342, -1.6493],\n",
      "        [ 0.1644, -0.1836,  0.0405],\n",
      "        [ 0.4860,  1.1370,  0.6028],\n",
      "        [ 1.0587,  0.0252,  1.1134]])\n",
      "tensor([[ 0.6145,  1.1860, -0.2074],\n",
      "        [ 0.9967,  1.4342, -1.6493],\n",
      "        [ 0.1644, -0.1836,  0.0405],\n",
      "        [ 0.4860,  1.1370,  0.6028],\n",
      "        [ 1.0587,  0.0252,  1.1134]])\n",
      "tensor([[ 0.6145,  1.1860, -0.2074],\n",
      "        [ 0.9967,  1.4342, -1.6493],\n",
      "        [ 0.1644, -0.1836,  0.0405],\n",
      "        [ 0.4860,  1.1370,  0.6028],\n",
      "        [ 1.0587,  0.0252,  1.1134]])\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "y = torch.rand(5, 3)\n",
    "print(x + y)  #方式1\n",
    "print(torch.add(x, y))  #方式2\n",
    "\n",
    "#方式3\n",
    "result = torch.empty(5, 3)\n",
    "torch.add(x, y, out=result)\n",
    "print(result)\n",
    "\n",
    "\n",
    "# adds x to y\n",
    "y.add_(x)\n",
    "print(y)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([-0.7824])\n",
      "-0.7824291586875916\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "x = torch.randn(1)\n",
    "print(x)\n",
    "print(x.item())\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
